//
//  HBQTabBarViewController.m
//  HelloBeeQuick
//
//  Created by LiYiLin on 16/8/18.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "HBQTabBarViewController.h"
#import "HBQHomeViewController.h"
#import "HBQSuperMarketViewController.h"
#import "HBQShoppingCartViewController.h"
#import "HBQMyselfViewController.h"
#import "HBQTabBarView.h"
#import "HBQTabBarButton.h"
#import <Masonry/Masonry.h>


#define KTABBAR_HEIGHT 49

@interface HBQTabBarViewController()

@property (nonatomic, strong) HBQTabBarView * tabBarView;


@end

@implementation HBQTabBarViewController


#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup_UI];
    
}


#pragma mark - 配置界面
- (void)setup_UI {
    
    self.tabBarView = [[HBQTabBarView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KTABBAR_HEIGHT)];
    [self.view addSubview:self.tabBarView];
    self.tabBar.hidden = YES;

    [self setup_Layout];
    [self setup_TabBarVC];
    [self receiveNotification];
}


#pragma mark - 添加约束
- (void)setup_Layout {

    [self.tabBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(KTABBAR_HEIGHT);
    }];
}


#pragma mark - 配置
- (void)setup_TabBarVC {

    [self addChildViewController:[[HBQHomeViewController alloc] init] withTitle:@"首页"];
    [self addChildViewController:[[HBQSuperMarketViewController alloc] init] withTitle:@"闪电超市"];
    [self addChildViewController:[[HBQShoppingCartViewController alloc] init] withTitle:@""];
    [self addChildViewController:[[HBQMyselfViewController alloc] init] withTitle:@"我"];
}

- (void)addChildViewController:(UIViewController *)childController withTitle:(NSString *)title {
    
    childController.title = title;
    [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:childController]];
}


#pragma mark - 接收传递按钮的通知
- (void)receiveNotification {
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didReceiveNotification:) name:TABBAR_BUTTON_NOTIFICATION object:nil];
}

- (void)didReceiveNotification:(NSNotification *)notification {
//    NSLog(@"跳转到购物车控制器");
    int tag = [notification.userInfo[@"tag"] intValue];
    //判断当前点击的是否是购物车按钮
    if(tag == 2) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[HBQShoppingCartViewController alloc] init]];
        [self presentViewController:nav animated:YES completion:nil];
        
        return;
    }
    self.selectedIndex = tag;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end














